<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>随手快递后台管理</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
        <link rel="stylesheet" href="../../css/global.css" media="all">
        <link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css">
        <link rel="stylesheet" href="../../datatables/media/css/jquery.dataTables.css">
        <link rel="stylesheet" href="../../datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css">
    </head>
    <body>
        <div class="layui-layout layui-layout-admin" style="border-bottom: solid 5px #1aa094;">
            <!--头部导航-->
            <div class="layui-header header header-demo" ></div>
            <!--左侧导航-->
            <div class="layui-side layui-bg-black" id="admin-side">
            </div>
            <!--右侧具体内容-->
            <div class="layui-body" style="bottom: 0;border-left: solid 2px #1AA094;padding:20px" id="admin-body">
                <div class="title">代金券管理</div>
                <a href="addvoucher.html"><button class="layui-btn layui-btn-small" style="float: right;margin-bottom: 10px"><i class="layui-icon">&#xe608;</i> 添加代金券</button></a>
                <hr/>
                <div class="layui-form">
                    <table class="layui-table" id="voucherTable">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="checkall" onchange="checkAll(this,'#voucherTable')"></th>
                                <th>代金券名称</th>
                                <th>金额</th>
                                <th>所需积分</th>
                                <th>有效期</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td><input type="checkbox" name="vouchercheck"></td>
                                <td>抵用券-积分兑换</td>
                                <td>4</td>
                                <td>1000</td>
                                <td>10天</td>
                                <td><div class="layui-btn layui-btn-danger  layui-btn-small voucherclose">删除</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit">编辑</div></td>
                            </tr>
                            <tr>
                                <td><input type="checkbox" name="vouchercheck"></td>
                                <td>抵用券-限时供应</td>
                                <td>9</td>
                                <td>1000</td>
                                <td>10天</td>
                                <td><div class="layui-btn layui-btn-danger  layui-btn-small voucherclose">删除</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit">编辑</div></td>
                            </tr>
                            <tr>
                                <td><input type="checkbox" name="vouchercheck"></td>
                                <td>抵用券-满30可抵</td>
                                <td>5</td>
                                <td>1000</td>
                                <td>10天</td>
                                <td><div class="layui-btn layui-btn-danger  layui-btn-small voucherclose">删除</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit">编辑</div></td>
                            </tr>
                        </tbody>
                    </table>
                    <div class="layui-btn layui-btn-danger  layui-btn-small closeall" id="vouchercloseall">批量删除</div>

                </div>
            </div>
            <!--底部导航-->
            <div class="layui-footer footer footer-demo" id="admin-footer"></div>
            <!--JS模块-->
            <script type="text/javascript" src="../../layui/layui.js"></script>
            <script src="../../js/jquery-1.12.3.min.js"></script>
            <script src="../../js/index.js"></script>
            <script type="text/javascript" src="../../js/jquery.cookie.js"></script>
            <!-- DataTables JavaScript -->
            <script src="../../datatables/media/js/jquery.dataTables.min.js"></script>
            <script type="text/javascript">
            var data = {
                "code": 200,
                "vouchers": [{
                    "vouchersId": 83011,
                    "vouchersName": "抵用券-限时供应",
                    "vouchersPrice": "4",
                    "vouchersDate": 7,
                    "vouchersScore": 1000,
                    // "vouchersState": 34597,
                    // "vouchersCreatetime": 77580115,
                    // "vouchersFailuretime": 383015465,
                    // "vouchersType": 7951
                }, {
                    "vouchersId": 83079,
                    "vouchersName": "抵用券-满30可抵",
                    "vouchersPrice": "5",
                    "vouchersDate": 15,
                    "vouchersScore": 1500,
                    // "vouchersState": 82316,
                    // "vouchersCreatetime": 65456456,
                    // "vouchersFailuretime": 334771561,
                    // "vouchersType": 28436
                }, {
                    "vouchersId": 83217,
                    "vouchersName": "抵用券-限时供应",
                    "vouchersPrice": "6",
                    "vouchersDate": 10,
                    "vouchersScore": 2000,
                    // "vouchersState": 31911,
                    // "vouchersCreatetime": 1516551165,
                    // "vouchersFailuretime": 5465415,
                    // "vouchersType": 50956
                }]
            }

            var table 
            $(function() {
                $("#voucher").addClass('layui-this'); //对应订单管理的背景色
                table = $('#voucherTable').DataTable({
                    responsive: true,
                    ordering: false,
                    searching: true,
                    "bLengthChange": false,
                    "bRetrieve": true,
                    "bFilter": true //过滤功能
                });
                /*$.ajax({
                                     type: "get",
                                     url: "/randomdelivery/VoucherController/vouchers/pageCount/1/pageSize/10,
                                     data: {},
                                     async: true,
                                     dataType: "json",
                                     contentType: "application/json",
                                     success: function(data) {*/
                                        voucherdata(data)
                                /*},
                                Error: function() {
                                    alert("服务器出错");
                                }
                            });*/
                adminCookie();
            })


            function voucher_Edit(voucherid) {
                window.location.href = "editvoucher.html?voucherid=" + voucherid;
            }


            $(document).on('click', '.voucherclose', function() {
                k = $(this).parents("tr").index();
                var voucherid = this.id
                var vouchersName = $(this).parents("tr").children('td:nth-child(2)').text()
                layui.use('layer', function() {
                    var layer = layui.layer;
                    layer.confirm('是否要删除抵用券：' + vouchersName + "?", {
                        icon: 2,
                        title: '提示'
                    }, function(index) {
                        /*$.ajax({
                            type: "get",
                            url: "/randomdelivery/VoucherController/voucher/" + voucherid,
                            data: {},
                            async: true,
                            dataType: "json",
                            contentType: "application/json",
                            success: function(data) {*/
                        if (data.code == 200) {
                            table.row("tr:eq(" + k + ")").remove().draw(false)
                            layer.close(index);
                            $("input[type='checkbox']:checked").prop('checked', false)
                        }
                        /*},
                            Error: function() {
                                alert("服务器出错");
                            }
                        })*/

                    });
                });
            });

                        $("#vouchercloseall").on('click', function() {
                            if ($("input[name='vouchercheck']:checked").length == 0) {
                                layer.msg("请选择抵用券")
                            } else {
                                layui.use('layer', function() {
                                    var layer = layui.layer;
                                    layer.confirm('是否要删除选中抵用券？', {
                                        icon: 2,
                                        title: '提示'
                                    }, function(index) {
                                        $("input[name='vouchercheck']:checked").each(function() {
                                            var element = this;
                                            if (!isNaN(element.id)) {

                                                /* $.ajax({
                                                     type: "DELETE",
                                                     url: "/randomdelivery/VoucherController/voucher/" + element.id,
                                                     data: {},
                                                     async: true,
                                                     dataType: "json",
                                                     contentType: "application/json",
                                                     success: function(data) {*/
                                                if (data.code == 200) {
                                                    m = $(this).parents("tr").index();
                                                    table.row("tr:eq(" + m + ")").remove().draw(false);
                                                }
                                                /*},
                                                    Error: function() {
                                                        alert("服务器出错");
                                                    }
                                                });*/
                                            }
                                        });
                                        layer.close(index);
                                        $("input[type='checkbox']:checked").prop('checked', false)
                                    });
                                });
                            }
                        })

                        $(document).on('click', 'tr td:not(:first)', function() {
                            if ($(this).siblings().first().children().is(':checked') == false) {
                                $(this).siblings().first().children().prop("checked", true);
                            } else {
                                $(this).siblings().first().children().prop("checked", false);
                            }
                        })

                        function voucherdata(data) {
                            $('#voucherTable').dataTable().fnClearTable(); //清除表格内
                            $.each(data.vouchers, function(index, item) {
                                var state
                                $('#voucherTable').dataTable().fnAddData([
                                    '<input type="checkbox" name="vouchercheck" id="'+item.vouchersId+'">',
                                    item.vouchersName,
                                    item.vouchersPrice,
                                    item.vouchersScore,
                                    item.vouchersDate,
                                    '<div class="layui-btn layui-btn-danger  layui-btn-small voucherclose" id="'+item.vouchersId+'">删除</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit" onclick="voucher_Edit('+item.vouchersId+')">编辑</div>'
                                ]);
                            })
                        }

            </script>
            
        </div>
    </body>
</html>